home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / CheckBoxDemo / CheckBoxDemo.cs next >
Encoding:
Text File  |  2001-01-15  |  1.8 KB  |  54 lines

  1. //-------------------------------------------
  2. // CheckBoxDemo.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class CheckBoxDemo: Form
  9. {
  10.      public static void Main()
  11.      {
  12.           Application.Run(new CheckBoxDemo());
  13.      }
  14.      public CheckBoxDemo()
  15.      {
  16.           Text = "CheckBox Demo";
  17.  
  18.           CheckBox[] achkbox  = new CheckBox[4];
  19.           int        cyText   = Font.Height;
  20.           int        cxText   = cyText / 2;
  21.           string[]   astrText = {"Bold", "Italic", "Underline", "Strikeout"};
  22.  
  23.           for (int i = 0; i < 4; i++)
  24.           {
  25.                achkbox[i] = new CheckBox();
  26.                achkbox[i].Text = astrText[i];
  27.                achkbox[i].Location = new Point(2 * cxText, 
  28.                                                (4 + 3 * i) * cyText / 2);
  29.                achkbox[i].Size = new Size(12 * cxText, cyText);
  30.                achkbox[i].CheckedChanged += 
  31.                               new EventHandler(CheckBoxOnCheckedChanged);
  32.           }
  33.           Controls.AddRange(achkbox);
  34.      }
  35.      void CheckBoxOnCheckedChanged(object obj, EventArgs ea)
  36.      {
  37.           Invalidate(false);
  38.      }
  39.      protected override void OnPaint(PaintEventArgs pea)
  40.      {
  41.           Graphics    grfx = pea.Graphics;
  42.           FontStyle   fs   = 0;
  43.           FontStyle[] afs  = { FontStyle.Bold,      FontStyle.Italic, 
  44.                                FontStyle.Underline, FontStyle.Strikeout };
  45.  
  46.           for (int i = 0; i < 4; i++)
  47.                if (((CheckBox) Controls[i]).Checked)
  48.                     fs |= afs[i];
  49.  
  50.           Font font = new Font(Font, fs);
  51.           grfx.DrawString(Text, font, new SolidBrush(ForeColor), 0, 0);
  52.      }
  53. }
  54.